package com.dev.ac.web.server;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dev.ac.annotation.Login;
import com.dev.ac.common.Const;
import com.dev.ac.common.Result;
import com.dev.ac.entity.AUser;
import com.dev.ac.service.AUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 付
 * @Date 2024-04-21 14:22
 * @Version 1.0
 * @description 用户管理
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
	@Autowired
	AUserService aUserService;

	@GetMapping("/list")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result list() {
		Object obj = aUserService.selectList();
		return Result.success(obj);
	}

	@GetMapping("/disable")
	public Result disable(Integer id) {
		AUser aUser = aUserService.getById(id);
		int i = 0;
		if (aUser.getIsDelete() == 0) {
			i = 1;
		}
		aUserService.update(new UpdateWrapper<AUser>()
				.eq("id", id)
				.set("is_delete", i)
		);
		return Result.success();
	}

	@GetMapping("/delete")
	public Result delete(Integer id) {
		AUser aUser = aUserService.getById(id);
		Assert.notNull(aUser, "抱歉，当前用户不存在");
		aUserService.remove(new QueryWrapper<AUser>().eq("id", id));
		return Result.success();
	}
}
